﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inovout
{
    public class DynamicDictionary : DynamicObject, IDictionary
    {
        public DynamicDictionary(Func<IDictionary> dynamicDictionaryThunk)
            : this(dynamicDictionaryThunk())
        {
        }
        public DynamicDictionary(IDictionary dictionary)
        {
            innerDictionary = dictionary;
        }
        private readonly IDictionary innerDictionary;
        protected IDictionary InnerDictionary { get { return innerDictionary; } }
         public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = innerDictionary[binder.Name];
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            innerDictionary[binder.Name] = value;
            // you can always set a key in the dictionary so return true
            return true;
        }

        public void Add(object key, object value)
        {
             innerDictionary.Add(key, value);
        }

        public void Clear()
        {
            innerDictionary.Clear();
        }

        public bool Contains(object key)
        {
            return innerDictionary.Contains(key);
        }

        public IDictionaryEnumerator GetEnumerator()
        {
            return innerDictionary.GetEnumerator();
        }

        public bool IsFixedSize
        {
            get { return innerDictionary.IsFixedSize; }
        }

        public bool IsReadOnly
        {
            get { return innerDictionary.IsReadOnly; }
        }

        public ICollection Keys
        {
            get { return innerDictionary.Keys; }
        }

        public void Remove(object key)
        {
            innerDictionary.Remove(key);
        }

        public ICollection Values
        {
            get { return innerDictionary.Values; }
        }

        public object this[object key]
        {
            get
            {
                return innerDictionary[key];
            }
            set
            {
                innerDictionary[key] = value;
            }
        }

        public void CopyTo(Array array, int index)
        {
            innerDictionary.CopyTo(array, index);
        }

        public int Count
        {
            get { return innerDictionary.Count; }
        }

        public bool IsSynchronized
        {
            get { return innerDictionary.IsSynchronized; }
        }

        public object SyncRoot
        {
            get { return innerDictionary.SyncRoot; }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}
